package com.gome.ocean.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gome.ocean.common.enums.ExecuteType;


/**
 * 根据指定时间获取毫秒值
 * 
 * @author haqiaolong 2016年1月22日 下午3:13:28
 * @since 5.0.0
 */
public final class TimeUtils {

    private TimeUtils(){
    }

    private static final Logger LOG            = LoggerFactory.getLogger(TimeUtils.class);
    public static final String  DEFAULT_FORMAT = "yyyy-MM-dd";

    /**
     * 根据制定的时间获取mills
     * 
     * @param currentTime
     * @param executeType
     * @return
     */
    public static long getTimeMills(Date dateTime, ExecuteType executeType) {
        Calendar calendar = Calendar.getInstance();
        Date current = dateTime;
        if (ExecuteType.EVERY_ONE_MINUTE == executeType) { // 每分钟
            calendar.setTime(current);
            calendar.add(Calendar.MINUTE, 1);
            return calendar.getTime().getTime();
        } else if (ExecuteType.EVERY_TEN_MINUTE == executeType) { // 每十分钟
            calendar.setTime(current);
            calendar.add(Calendar.MINUTE, 10);
            return calendar.getTime().getTime();
        } else if (ExecuteType.EVERY_FIVE_MINUTE == executeType) { // 每五分钟
            calendar.setTime(current);
            calendar.add(Calendar.MINUTE, 5);
            return calendar.getTime().getTime();
        } else if (ExecuteType.EVERY_THIRTY_MINUTE == executeType) {
            calendar.setTime(current);
            calendar.add(Calendar.MINUTE, 30);
            return calendar.getTime().getTime();
        } else if (ExecuteType.EVERY_ONE_HOUR == executeType) { // 每小时
            calendar.setTime(current);
            calendar.add(Calendar.MINUTE, 60);
            return calendar.getTime().getTime();
        } else if (ExecuteType.EVERY_ONE_DAY == executeType) { // 每天
            calendar.setTime(current);
            calendar.add(Calendar.DATE, 1);
            return calendar.getTime().getTime();
        } else {
            return current.getTime();
        }
    }

    /**
     * 根据指定时间戳转换时间
     * 
     * @param beginDate
     * @param format
     * @return
     */
    public static String timestampToDate(long beginDate) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = dateFormat.format(new Date(beginDate));
        return dateTime;
    }
    
    /**
     * 获取今天时间 年月日 时分秒
     * @return
     */
    public static String timestampToToday(){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = dateFormat.format(new Date());
        return dateTime;
    }

    /**
     * 今天
     * 
     * @return
     */
    public static String today() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = dateFormat.format(new Date());
        return dateTime;
    }

    /**
     * 昨天
     * 
     * @return
     */
    public static String yesterday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
        return yesterday;
    }

    /**
     * 明天
     * 
     * @return
     */
    public static String tomorrow() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, +1);
        String tomorrow = new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
        return tomorrow;
    }

    public static Date getDateTime(String dateTime) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dateFormat.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
            LOG.error("日期格式化错误!", e);
            return null;
        }
    }
}
